<template>
	<form-container
		:title="formData.id ? '编辑检验方案信息' : '新增检验方案信息'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入名称" allow-clear />
			</a-form-item>
			<a-form-item label="类型：" name="type">
				<a-select v-model:value="formData.type" placeholder="请选择分类" :options="TypeOptions" allow-clear></a-select>
			</a-form-item>
			<a-form-item label="检验方式：" name="manner">
				<a-select v-model:value="formData.manner" placeholder="请选择分类" :options="MedthOptions" allow-clear></a-select>
			</a-form-item>
			<a-form-item label="值：" name="value">
				<a-input v-model:value="formData.value" placeholder="请输入备注" allow-clear />
			</a-form-item>
			<a-form-item label="描述：" name="desc">
				<a-input v-model:value="formData.desc" placeholder="请输入备注" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-checkbox class="mr-2" v-if="!formData.id" v-model:checked="addNext">继续新增下一条</a-checkbox>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="positionForm">
import { required } from '@/utils/formRules'
import { getCurrentInstance, ref } from 'vue'
import tool from '@/utils/tool'
import protocol from '@/api/mes/basicdata/protocol'


	// 定义emit事件
	const emit = defineEmits({ successful: null })
	const { proxy } = getCurrentInstance()
	// 默认是关闭状态
	let visible = ref(false)
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	// 定义机构元素
	const treeData = ref([])
	const submitLoading = ref(false)
	// 继续新增下一条
const addNext = ref(false)

//类型
    const TypeOptions = tool.dictList('PROTOCOL')
	//方式
    const MedthOptions = tool.dictList('INSMTHOD')

	// 打开抽屉
	const onOpen = (record, orgId) => {
		visible.value = true
		addNext.value = false
		formData.value = {
			name: formData.value.name,
			type: formData.value.type,
			manner: formData.value.manner,			
			value: formData.value.value,			
			desc: formData.value.desc,			
		}
		if (record) {
			formData.value = Object.assign({}, record)
		}
		
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		sName: [required('请输入学生名称')],
		age: [required('请输入年龄')],
    }
	//类型
	const processTypeOptions = tool.dictList('PROCESS_TYPE')
    // 报工模式
	const processModeOptions = tool.dictList('PROCESS_MODE')

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			protocol
				.protocolAdd(formData.value, formData.value.id)
				.then(() => {
					if (addNext.value) {
						formData.value = {
							name: formData.value.name,
							type: formData.value.type,
							manner: formData.value.manner,			
							value: formData.value.value,			
							desc: formData.value.desc,
						}
					} else {
						visible.value = false
					}
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
